﻿package framework.core;


public class SearchEventRule 
{
	//interface simple
	public Condition getCondition(String sql) throws Exception
	{
		return new Condition(sql);
	}
	public Condition getCondition(String fieldName,int v) throws Exception
	{
		return getCondition(Conditions.EQUAL,fieldName,v); 
	}
	public Condition getCondition(String fieldName,double v) throws Exception
	{
		return getCondition(Conditions.EQUAL,fieldName,v);
	}
	public Condition getCondition(String fieldName,String v) throws Exception
	{
		return getCondition(Conditions.EQUAL,fieldName,v);
	}
	public Condition getCondition(String fieldName,Value v) throws Exception
	{
		return getCondition(Conditions.EQUAL,fieldName,v);
	}
	
	public Condition getCondition(int type,String fieldName,int v) throws Exception
	{
		if(type!=Conditions.CUSTOM && v==Value.NULL_INT) return null; 
		String sql = Conditions.getConditionSql(fieldName,type,1);
		return new Condition(sql,v);
	}
	public Condition getCondition(int type,String fieldName,double v) throws Exception
	{
		if(type!=Conditions.CUSTOM && v==Value.NULL_DOUBLE) return null; 		
		String sql = Conditions.getConditionSql(fieldName,type,1);
		return new Condition(sql,v);
	}
	public Condition getCondition(int type,String fieldName,String v) throws Exception
	{
		if(type!=Conditions.CUSTOM)
		{
			if(v==null || v.equals("")) return null;
		} 
		if(type==Conditions.EQUAL)
		{
			if(v.indexOf("%")!=-1 || v.indexOf("_")!=-1)
			{
				type = Conditions.LIKE ;
			}
		}
		if(type==Conditions.LIKE)
		{
			v = "%"+v.toUpperCase()+"%";
			fieldName = "upper("+fieldName+")";
		}
		String sql = Conditions.getConditionSql(fieldName,type,1);
		return new Condition(sql,v);
	}
	public Condition getCondition(int type,String fieldName,Value v) throws Exception
	{
		if(v.isString()) return getCondition(type,fieldName,v.getString());
		String sql = Conditions.getConditionSql(fieldName,type,1);
		return new Condition(sql,v);
	}
	public Condition getCondition(int type,String fieldName,int[] v) throws Exception
	{
		int paramCount = (v==null) ? 0 : v.length ;
		String sql = Conditions.getConditionSql(fieldName,type,paramCount);
		return new Condition(sql,v);
	}
	public Condition getCondition(int type,String fieldName,double[] v) throws Exception
	{
		int paramCount = (v==null) ? 0 : v.length ;
		String sql = Conditions.getConditionSql(fieldName,type,paramCount);
		return new Condition(sql,v);
	}
	public Condition getCondition(int type,String fieldName,String[] v) throws Exception
	{
		int paramCount = (v==null) ? 0 : v.length ;
		String sql = Conditions.getConditionSql(fieldName,type,paramCount);
		return new Condition(sql,v);
	}
	public Condition getCondition(int type,String fieldName,Values v) throws Exception
	{
		int paramCount = (v==null) ? 0 : v.size();
		String sql = Conditions.getConditionSql(fieldName,type,paramCount);
		return new Condition(sql,v);
	}
}
